<?php
namespace app\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class DynamicData extends Basic
{
    
    public static function tableName()
    {
        return '{{%dynamic_data}}';
    }

    public function rules()
    {
        return [
            ['app_id', 'required', 'message' => 'app_id不能为空', 'on' => 'add'],
            ['app_id', 'integer', 'message' => 'app_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'app_id最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'app_id最大存储18446744073709551615'],
            ['name', 'required', 'message' => 'name不能为空', 'on' => 'add'],
            ['name', 'string', 'message' => 'name必须是字符串',
                        'min' => 1, 'tooShort' => 'name最短1位',
                        'max' => 16, 'tooLong' => 'name最长16位'],
            ['defaultflagnum', 'integer', 'message' => 'defaultflagnum必须为数字，或数字字符串', 
                         'min'=>0, 'tooSmall' => 'defaultflagnum最小存储0', 
                         'max'=>65535, 'tooBig' => 'defaultflagnum最大存储65535'],
            ['is_member', 'integer', 'message' => 'is_member必须为数字，或数字字符串'],
            ['is_member', 'in', 'range' => array(1, 2), 'message' => 'is_member值必须在1,2中选择'], 
            ['send_integral', 'integer', 'message' => 'send_integral必须为数字，或数字字符串'],
            ['send_integral', 'in', 'range' => array(1, 2), 'message' => 'send_integral值必须在1,2中选择'],
            ['integral', 'integer', 'message' => 'integral必须为数字，或数字字符串', 
                         'min'=>0, 'tooSmall' => 'integral最小存储0', 
                         'max'=>65535, 'tooBig' => 'integral最大存储65535'],
            ['max_integral', 'integer', 'message' => 'max_integral必须为数字，或数字字符串', 
                         'min'=>0, 'tooSmall' => 'max_integral最小存储0', 
                         'max'=>65535, 'tooBig' => 'max_integral最大存储65535'],
            ['max_times', 'integer', 'message' => 'max_times必须为数字，或数字字符串', 
                         'min'=>0, 'tooSmall' => 'max_times最小存储0', 
                         'max'=>65535, 'tooBig' => 'max_times最大存储65535'],
            ['data_count', 'integer', 'message' => 'data_count必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'data_count最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'data_count最大存储18446744073709551615'],
            ['sort', 'integer', 'message' => 'sort必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'sort最小存储0', 
                        'max'=>18446744073709551615, 'tooBig' => 'sort最大存储18446744073709551615'],
            ['audit', 'integer', 'message' => 'audit必须为数字，或数字字符串'],
            ['audit', 'in', 'range' => array(1, 2), 'message' => 'audit值必须在1,2中选择'],
            ['can_reply', 'integer', 'message' => 'can_reply必须为数字，或数字字符串'],
            ['can_reply', 'in', 'range' => array(1, 2), 'message' => 'can_reply值必须在1,2中选择'],
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(1, 2), 'message' => 'display值必须在1,2中选择'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00', 'on' => 'add'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],   
        ];
    }


    /*添加*/
    public function addDynamicData($data, $app_id)
    {
        // P($data);changeCatidFormat
        $this->scenario = 'add';
        $data['DynamicData']['app_id'] = $app_id;
        if(isset($data['DynamicData']['is_member'])){
            $data['DynamicData']['is_member'] = Tool::changeBool($data['DynamicData']['is_member']);
        }
        if(isset($data['DynamicData']['send_integral'])){
            $data['DynamicData']['send_integral'] = Tool::changeBool($data['DynamicData']['send_integral']);
        }
        $data['DynamicData']['add_time'] = date('Y-m-d H:i:s', time());
        if(isset($data['DynamicData']['audit'])){
            $data['DynamicData']['audit'] = Tool::changeBool($data['DynamicData']['audit']);
        }
        if(isset($data['DynamicData']['can_reply'])){
            $data['DynamicData']['can_reply'] = Tool::changeBool($data['DynamicData']['can_reply']);
        }
        if(isset($data['DynamicData']['display'])){
            $data['DynamicData']['display'] = Tool::changeBool($data['DynamicData']['display']);
        }
        /*因为添加的时候，传过来的是数组，添加之后要跟新，所以这里默认为0*/
        $data['DynamicData']['defaultflagnum'] = 0;
        // P($data);
        if($this->load($data) and $this->validate()){
            // P($this);
            if($this->save(false)){
                $id = $this->getPrimaryKey();
                $this->updateAll(['sort' => $id], 'dynamic_id = :id', [':id' => $id]);
                return $id;
            }
        }
        return false;
    }

    /*
    修改
    $data       修改的数据
    $app_id 
    */
    public function modifyDynamicData($data, $id)
    {
        // P($data);
        $dynamicData = self::find()->where('dynamic_id = :id', [':id' => $id])->one();
        // P($dynamicData);
        if(isset($data['DynamicData']['is_member'])){
            $data['DynamicData']['is_member'] = Tool::changeBool($data['DynamicData']['is_member']);
        }
        if(isset($data['DynamicData']['send_integral'])){
            $data['DynamicData']['send_integral'] = Tool::changeBool($data['DynamicData']['send_integral']);
        }
        if(isset($data['DynamicData']['audit'])){
            $data['DynamicData']['audit'] = Tool::changeBool($data['DynamicData']['audit']);
        }
        if(isset($data['DynamicData']['can_reply'])){
            $data['DynamicData']['can_reply'] = Tool::changeBool($data['DynamicData']['can_reply']);
        }
        if(isset($data['DynamicData']['display'])){
            $data['DynamicData']['display'] = Tool::changeBool($data['DynamicData']['display']);
        }
        // P($data);
        if(!is_null($dynamicData)){
            if($this->load($data) and $this->validate()){
                if(isset($this->name)){
                    $dynamicData->name = $this->name;
                }
                if(isset($this->is_member)){
                    $dynamicData->is_member = $this->is_member;
                }
                if(isset($this->defaultflagnum)){
                    $dynamicData->defaultflagnum = $this->defaultflagnum;
                }
                if(isset($this->send_integral)){
                    $dynamicData->send_integral = $this->send_integral;
                }
                if(isset($this->integral)){
                    $dynamicData->integral = $this->integral;
                }
                if(isset($this->max_integral)){
                    $dynamicData->max_integral = $this->max_integral;
                }
                if(isset($this->max_times)){
                    $dynamicData->max_times = $this->max_times;
                }
                if(isset($this->data_count)){
                    $dynamicData->data_count = $this->data_count;
                }
                if(isset($this->audit)){
                    $dynamicData->audit = $this->audit;
                }
                if(isset($this->can_reply)){
                    $dynamicData->can_reply = $this->can_reply;
                }
                if(isset($this->display)){
                    $dynamicData->display = $this->display;
                }
                if($dynamicData->save(false)){
                    return true;
                }
            }
        };
        return false;
    }


    /*获取字段信息*/
    public function getAttr()
    {
        $attr = $this->hasMany(DynamicDataAttr::className(), ['dynamic_id' => 'dynamic_id'])
                    ->select(['{{%dynamic_data_attr}}.dynamic_id', 'attr_name', 'attr_enname', 'attr_type', 'attr_value', 'can_null', 'default_val'])
                    ->where('state = 2');
        return $attr;
    }

    /*获取标示信息*/
    public function getFlag()
    {
        $flag = $this->hasOne(DynamicDataFlag::className(), ['dynamic_id' => 'dynamic_id']);
        return $flag;
    }

    
}